<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar title="登录" class="page-nav">
      <!-- 关闭登录 点击x路由后退页面-->
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="refForm">
      <van-field
        name="mobile"
        v-model="user.mobile"
        :rules="rulesForm.mobile"
        type="number"
        required
        clearable
        label="手机号"
        placeholder="请输入手机号"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        name="code"
        v-model="user.code"
        :rules="rulesForm.code"
        type="number"
        label="验证码"
        placeholder="请输入验证码"
        required
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <!-- time 属性表示倒计时总时长，单位为毫秒。 -->
          <!-- finish 倒计时结束时触发 -->
          <!-- 通过 format 属性设置倒计时文本的内容。 -->
          <!-- <van-count-down :time="time" format="DD 天 HH 时 mm 分 ss 秒" /> -->
          <!-- 根据接口文档,验证码倒计时时长改为60s -->
          <van-count-down
            :time="1000 * 60"
            format="ss s"
            v-if="isCountDownShow"
            @finish="isCountDownShow = false"
          />
          <van-button
            class="send-sms-btn"
            native-type="button"
            round
            size="small"
            type="default"
            @click="onSend"
            v-else
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendCode } from '@/api/user'
// import { login } from '@/api/user'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data () {
    return {
      // 是否展示倒计时
      isCountDownShow: false,
      // 用于存储用户信息
      user: {
        mobile: '13911111111', // 手机号 13911111111
        code: '246810' // 验证码  246810
      },
      // 表单校验
      rulesForm: {
        mobile: [
          { required: true, message: '请填写用户名' },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '手机号格式错误' }
        ],
        code: [
          { required: true, message: '请填写用户名' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      }
    }
  },
  methods: {
    // 登录提交
    async onSubmit () {
      // console.log('submit', values)
      // 获取表单数据
      const user = this.user
      // toast轻提示
      this.$toast.loading({
        message: '登录中...', // 消息提示
        forbidClick: true, // 禁用背景点击
        duration: 0 // 持续时间,设置为0则持续展示
      })
      try {
        // 解构
        const { data: res } = await login(user)
        this.$store.commit('getUser', res.data)
        console.log(res, '登陆成功')
        this.$toast.success('登录成功')
        // 登陆成功,跳转到个人主页
        this.$router.push('/my')
      } catch (error) {
        if (error.response.status === 400) {
          // console.log('状态错误')
          this.$toast.fail('手机号或验证码错误')
        } else {
          console.log(error, '登录失败')
          this.$toast.fail('登录失败')
        }
      }
    },
    // 获取验证码
    async onSend () {
      // console.log('获取到验证码')
      try {
        // 1.校验手机号
        await this.$refs.refForm.validate('mobile')
        console.log('验证成功')
      } catch (error) {
        return console.log('验证失败', error)
      }
      // console.log('我会被执行吗?')
      // 2.验证通过,显示倒计时
      this.isCountDownShow = true
      // 3.请求发送验证码
      try {
        const res = await sendCode(this.user.mobile)
        console.log(res, '验证码发送成功')
        this.$toast.success('验证码登录成功')
      } catch (error) {
        // 200 OK
        // 404 手机号不正确
        // 429 接口访问次数受限 ， body数据返回
        // 429 (Too Many Requests)太多请求发送
        if (error.message.status === 429) {
          this.$toast.fail('验证码发送过于频繁,请稍后再试')
        } else {
          this.$toast.fail('验证码发送失败哦')
        }
        // console.log('验证码发送失败')
      }
    }
  }
  // computed: {},
  // watch: {},
  // created () {},
  // mounted () {}
}
</script>
<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 160px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
  .page-nav {
  background-color: #3296fa;
  .van-nav-bar__title, .van-icon {
    color: #fff;
  }
}
}
</style>
